package com.zzl.study.auth.security.handler;

import cn.hutool.json.JSONUtil;
import com.zzl.study.auth.domain.Response;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * @author: zhangzl
 * @date: 2024/6/17 14:13
 * @version: 1.0
 * @description:
 */
@Slf4j
public class OidcOAuth2LogoutSuccessHandler implements LogoutSuccessHandler {

    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        log.info("用户 {} 登出成功!", authentication.getPrincipal());
        Response<String> restResponse = Response.success("登出成功!");
        response.setCharacterEncoding(StandardCharsets.UTF_8.name());
        response.setContentType(MediaType.APPLICATION_JSON_VALUE);
        response.getWriter().write(JSONUtil.toJsonStr(restResponse));
        response.getWriter().flush();
    }

}
